import { request } from '../request';

export interface OwenPlanList {
  planId: string;
  name: string;
}

/**
 * @desc 获取自己的周期计划名称列表（企业和协会公用）
 */
export async function getOwnCoursePlanList(entId: string) {
  const { data } = await request<OwenPlanList[]>({
    url: '/safe/api/statistics/ownCoursePlanList',
    params: {
      entId,
    },
  });
  return data;
}

/**
 * @desc 企业端获取协会的周期计划名称列表（其他）
 */
export async function getXiehuiPlanList(entId: string) {
  const { data } = await request<OwenPlanList[]>({
    url: '/safe/api/statistics/noOwnCoursePlanList',
    params: {
      entId,
    },
  });
  return data;
}

export interface CycleList {
  cycleId: string;
  beginTime: string;
  endTime: string;
}
export async function getCycleByPlan(planId: string) {
  const { data } = await request<CycleList[]>({
    url: '/safe/api/safePlanCourse/getCycleByPlan',
    params: {
      planId,
    },
  });
  return data;
}

export interface HistoryListSearchParams {
  cycleId?: string;
  personId: string;
  entId?: string;
  entName?: string;
  finishState?: string | number;
  planId?: string;
  entSource?: number;
  source?: number;
  curEntId?: string;
  planOption?: string[];
  pageIndex?: number;
  pageSize?: number;
}

export interface HistoryListItem {
  cycleTime: string;
  endTime: string;
  entId: string;
  entName: string;
  finishState: number;
  personName: string;
  phone: string;
  planCourseId: string;
  startTime: string;
  title: string;
  finishTime: string;
  name: string;
  source: number;
  planEntName: string;
  No: number;
  extraInfo?: { title: string; value: string }[];
  joinList?: { name: string }[];
}

/**
 * @desc 分页查询历史学习情况
 */
async function getHistoryStudyDetailList(
  url: string,
  { cycleId, planId, finishState, entId, curEntId }: Partial<HistoryListSearchParams>,
  pageIndex = 1,
  pageSize = 10,
) {
  const { data } = await request<{ list: HistoryListItem[]; total: number }>({
    url,
    data: {
      pageIndex,
      pageSize,
      cycleId,
      planId,
      finishState,
      entId,
      curEntId,
    },
    noAutoShowErrorMessage: true,
  });
  data.list.forEach((e, index) => {
    e.No = index + 1;
  });
  return data;
}

//协会导出历史数据
function exportHistoryList({
  entId,
  entName,
  planId,
  cycleId,
  finishState,
  entSource,
  source,
}: HistoryListSearchParams) {
  const downUrl =
    location.origin +
    `/safe/api/statistics/historyStudyDetailExport?entId=${entId}&planId=${planId}&cycleId=${cycleId}&finishState=${
      finishState || ''
    }&pageIndex=0&pageSize=999999`;
  console.log('downUrl', downUrl);
  // const a = document.createElement('a');
  // a.target = '_blank';
  // a.href = downUrl;
  // a.click();
  window.open(downUrl);
}

/**
 * 导出
 *
 * @param url
 * @param param1
 */
function exportStudySituationDataList(
  url: string,
  {
    personId,
    entId,
    entName,
    planId,
    cycleId,
    finishState,
    entSource,
    source,
    curEntId,
    pageIndex,
    pageSize,
  }: HistoryListSearchParams,
) {
  const downUrl =
    location.origin +
    `${url}?entId=${entId}&curEntId=${curEntId || ''}&planId=${planId}&cycleId=${cycleId}&finishState=${
      finishState || ''
    }&pageIndex=${pageIndex || 1}&pageSize=${pageSize || 9999}`;

  window.open(downUrl);
}
const assHistoryStudyDetailExportNew = (params: HistoryListSearchParams) => {
  return request({
    method: 'get',
    url: '/safe/api/statistics/assHistoryStudyDetailExportNew',
    params,
    noAuthorization: true,
  });
};
const entHistoryStudyDetailExportNew = (params: HistoryListSearchParams) => {
  return request({
    method: 'get',
    url: '/safe/api/statistics/entHistoryStudyDetailExportNew',
    params,
    noAuthorization: true,
  });
};
export {
  exportHistoryList,
  exportStudySituationDataList,
  assHistoryStudyDetailExportNew,
  entHistoryStudyDetailExportNew,
  getHistoryStudyDetailList,
};
